<?php
class ServerException extends RuntimeException
{
	protected $name = "";

	public function __construct()
	{
		$this->name = get_class($this);
	}

	public function toJSON()
	{
		$object = array();

		$object["line"] = $this->getLine();
		$object["source"] = $this->getFile();
		$object["code"] = $this->getCode();
		$object["message"] = $this->getMessage();
		$object["trace"] = $this->getTrace();
		$object["name"] = $this->name;

		return( json_encode($object) );
	}

	public function toString($userMode = false)
	{
		// TODO Mostrar errores en -modo usuario-.
		//
		$source = $this->source ? $this->source : $this->file;
		$header = "<strong>" . str_replace("%1", $this->code ? " $this->code" : null, $this->type) . "</strong>";
		$strErr = "$header $this->message in <strong>$source</strong>.";

		return($strErr);
	}

	public function getTraceString()
	{
		$stack = $this->getTrace();
		$str = $this->name . "\n";

		foreach( $stack as $exception )
		{
			$str .= "\tat" . $exception->name . "(" . $exception->getFile() . ":" . $exception->getLine() . ")\n";
		}

		return($str);
	}
}
?>
